package myzzyl.service.Impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import myzzyl.constants.ErrorConstants;
import myzzyl.domain.dto.NursingProjectDto;
import myzzyl.domain.pojo.NursingProject;
import myzzyl.domain.vo.NursingProjectVo;
import myzzyl.exception.BusinessException;
import myzzyl.response.BaseResponse;
import myzzyl.response.PageResponse;
import myzzyl.service.NursingProjectService;
import myzzyl.mapper.NursingProjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 33249
* @description 针对表【nursing_project(护理项目表)】的数据库操作Service实现
* @createDate 2025-08-10 10:20:15
*/
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject>
    implements NursingProjectService{


    /**
     * 分页获取所有护理项目
     * @param name 项目名称
     * @param status 项目状态
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 返回值
     */
    @Override
    public PageResponse<NursingProjectVo> getAll(String name, Integer status, Integer pageNum, Integer pageSize) {
        // 创建分页对象
        Page<NursingProject> page = new Page<>(pageNum, pageSize);

        // 添加排序字段
        page.addOrder(OrderItem.asc("order_no"), OrderItem.desc("create_time"));

        // 自定义分页查询
        IPage<NursingProjectVo> nursingProjectVoIPage = baseMapper.selectNursingProjectVo(page, name, status);

        // 封装分页响应格式对象返回
        return new PageResponse<>(nursingProjectVoIPage.getPages(), nursingProjectVoIPage.getTotal(),
                nursingProjectVoIPage.getRecords());
    }


    /**
     * 获取护理项目详情
     * @param id 护理项目id
     * @return 返回值
     */
    @Override
    public NursingProjectVo getInfo(Long id) {
        // 查询护理项目是否存在
        NursingProject nursingProject = getById(id);
        if(nursingProject == null) {
            throw new BusinessException(ErrorConstants.NURSING_PROJECT_IS_NOT_EXIST);
        }
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }


    /**
     * 新增护理项目
     * @param nursingProjectDto 护理项目表单请求包装类
     * @return 返回值
     */
    @Override
    public boolean add(NursingProjectDto nursingProjectDto) {
        // 判断护理项目名是否为空
        if (StrUtil.isBlank(nursingProjectDto.getName())) {
            throw new BusinessException(ErrorConstants.NURSING_PROJECT_NAME_IS_NULL);
        }

        // 查询护理项目是否已经存在
        if (lambdaQuery().eq(NursingProject::getName, nursingProjectDto.getName()).exists()) {
            throw new BusinessException(ErrorConstants.NURSING_PROJECT_IS_ALREADY_EXIST);
        }

        // 新增护理项目
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);

        return save(nursingProject);
    }


    /**
     * 编辑护理项目
     * @param nursingProjectDto 护理项目表单请求包装类
     * @return 返回值
     */
    @Override
    public boolean edit(NursingProjectDto nursingProjectDto) {
        // 判断护理项目名是否为空
        if (StrUtil.isBlank(nursingProjectDto.getName())) {
            throw new BusinessException(ErrorConstants.NURSING_PROJECT_NAME_IS_NULL);
        }

        // 查询护理项目是否已经存在（除去自己）
        if (lambdaQuery()
                .eq(NursingProject::getName, nursingProjectDto.getName())
                .ne(NursingProject::getId, nursingProjectDto.getId())
                .exists()) {
            throw new BusinessException(ErrorConstants.NURSING_PROJECT_IS_ALREADY_EXIST);
        }

        // 更新护理项目
        return lambdaUpdate()
                .set(StrUtil.isNotBlank(nursingProjectDto.getName()), NursingProject::getName, nursingProjectDto.getName())
                .set(StrUtil.isNotBlank(nursingProjectDto.getImage()), NursingProject::getImage, nursingProjectDto.getImage())
                .set(StrUtil.isNotBlank(nursingProjectDto.getNursingRequirement()), NursingProject::getNursingRequirement, nursingProjectDto.getNursingRequirement())
                .set(StrUtil.isNotBlank(nursingProjectDto.getUnit()), NursingProject::getUnit, nursingProjectDto.getUnit())
                .set(nursingProjectDto.getOrderNo() != null, NursingProject::getOrderNo, nursingProjectDto.getOrderNo())
                .set(nursingProjectDto.getStatus() != null, NursingProject::getStatus, nursingProjectDto.getStatus())
                .set(nursingProjectDto.getPrice() != null, NursingProject::getPrice, nursingProjectDto.getPrice())
                .eq(NursingProject::getId, nursingProjectDto.getId())
                .update();
    }


    /**
     * 删除护理项目
     * @param id 护理项目id
     * @return 返回值
     */
    @Override
    public boolean deleteNursingProjectById(Long id) {
        // 查询护理项目是否存在
        if(!lambdaQuery().eq(NursingProject::getId, id).exists()) {
            throw new BusinessException(ErrorConstants.NURSING_PROJECT_IS_NOT_EXIST);
        }

        // 删除护理项目
        return baseMapper.deleteById(id) > 0;
    }


    /**
     * 启用或禁用护理项目
     * @param id 护理项目id
     * @param status 需要更新的护理项目状态
     * @return 返回值
     */
    @Override
    public boolean enableOrDisableNursingProject(Long id, Integer status) {
        // 查询护理项目是否存在
        if(!lambdaQuery().eq(NursingProject::getId, id).exists()) {
            throw new BusinessException(ErrorConstants.NURSING_PROJECT_IS_NOT_EXIST);
        }

        // 更新护理项目状态
        return lambdaUpdate()
                .set(status != null, NursingProject::getStatus, status)
                .eq(NursingProject::getId, id)
                .update();
    }


    /**
     * 获取所有护理项目
     * @return 返回值
     */
    @Override
    public List<NursingProjectVo> listAll() {
        // 复用已有的分页查询接口，但是不传入分页相关参数
        Page<NursingProject> page = new Page<>();

        // 添加排序字段
        page.addOrder(OrderItem.asc("order_no"), OrderItem.desc("create_time"));

        // 直接调用已有的分页查询
        return baseMapper.selectNursingProjectVo(page, null, null).getRecords();
    }
}




